package code.ws_common_scene.manager
{
	import code.ws_common_ems.ModeStatusEms;
	import code.ws_common_scene.GameScene;
	import code.ws_common_scene.command.HeroCommandDevice;
	import code.ws_common_scene.command.cmd.AttackCommand;
	import code.ws_common_scene.command.cmd.TransCommand;
	import code.ws_common_scene.command.cmd.WalkCommand;
	import code.ws_common_scene.eft.Effects;
	import code.ws_common_scene.mode.Monster;
	import code.ws_common_scene.mode.Trans;
	import code.ws_common_scene.mode.base.BRole;
	import code.ws_common_scene.mode.base.Fighter;
	import code.ws_common_scene.mode.inters.ISprite;
	import code.ws_common_utils.IDUtils;
	import code.ws_common_utils.LayerUtils;
	
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import starling.display.DisplayObject;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;

	/**
	 * 场景管理器 （单例类）
	 * @author qiangchen
	 * 
	 */	
	public class SceneManager
	{
		/** 唯一实例对象标识符 */
		private static var _instance:SceneManager = null;
		/** 场景引用对象 */
		private var _scene:GameScene = null;
		/** 场景点击频率 */
		private var _clickTime:Number = NaN;
		/** 场景触屏开关 */
		private var _sceneTouchFlag:Boolean = false;
		/** 当前的英雄攻击的目标id */
		private var _crtAttackid:int = IDUtils.INVALID_ID;
		/** 当前选中目标id */
		private var _crtSelectid:int = IDUtils.INVALID_ID;
		/** 上一次选中的目标id */
		private var _prevSelectid:int = IDUtils.INVALID_ID;
		
		
		/**
		 * 构造函数 
		 * 
		 */		
		public function SceneManager()
		{
			if( _instance )
			{
				throw new Error("[SceneManager]--->(Error): This class is only in Memery!");
				return;
			}
			_instance = this;
			
			// 初始化
			initer();
		}
		
		
		/**
		 * 创建唯一实例对象 
		 * @return 
		 * 
		 */		
		public static function get instance():SceneManager
		{
			if( _instance == null )
			{
				_instance = new SceneManager();
			}
			return _instance;
		}
		
		
		/**
		 * 初始化 
		 * 
		 */		
		private function initer():void
		{
			_scene = LayerUtils.scene;
			_scene.addEventListener( TouchEvent.TOUCH, sceneTouchHandler );
		}
		
		
		/**
		 * 手指触摸场景
		 * @param evt
		 * 
		 */		
		private function sceneTouchHandler( evt:TouchEvent ):void
		{
			if( !_sceneTouchFlag ) return;
			
			var touch:Touch = evt.getTouch( _scene );
			if( touch == null )
			{
				return;
			}
			
			// 手指触摸按下
			if( touch.phase == TouchPhase.ENDED )
			{
				if( getTimer() - _clickTime < 160 )
				{
					// 点击太快
					touch = null;
					return;
				}
				_clickTime = getTimer();
				
				var target:DisplayObject = getTouchTarget( touch );
				if( target is Fighter && ( target as Fighter ).status == ModeStatusEms.DEAD )
				{
					target = null;
				}
				if( target == null )
				{
					// 释放攻击目标
					crtAttackid = IDUtils.INVALID_ID;
					// 取消选中状态和记录的目标id
					var article:BRole = scene._eyeDic[ crtSelectid ] as BRole;
					article && article.removeSelect();
					article = null;
					crtSelectid = prevSelectid = IDUtils.INVALID_ID;
					
					// 寻路置指引目标点
					var end_p:Point = _scene.globalToLocal( new Point( touch.globalX, touch.globalY ) );
					HeroCommandDevice.instance.updataCommand( new WalkCommand( end_p ) );
					_scene.addEffect( "shubiaozhiyin", new Effects( "shubiaozhiyin", "shubiaozhiyin" ) , end_p );	
					end_p = null;
				}
				else
				{
					if( target is Trans )
					{
						// 点击的传送阵
						HeroCommandDevice.instance.updataCommand( new TransCommand( target as Trans ) );
					}
					
					// 设置选中目标对象
					selectArtical( target as ISprite );
				}
	
				// 释放
				target = null;
			}
			touch = null;
		}
		
		
		/**
		 * 选中场景表演者 
		 * @param target
		 * 
		 */		
		private function selectArtical( target:ISprite ):void
		{
			if( !(target is Monster) ) return;
			
			if( crtSelectid != target.showid )
			{
				crtSelectid = target.showid;
				crtAttackid = IDUtils.INVALID_ID;
				
				if( crtSelectid != prevSelectid && prevSelectid != IDUtils.INVALID_ID )
				{
					// 删除上一次选中目标的标识符
					var article:BRole = scene._eyeDic[ prevSelectid ] as BRole;
					article && article.removeSelect();
					article = null;
				}
				
				// 添加当前选中目标的标识符
				( target as BRole ).addSelect();
				
				prevSelectid = target.showid;
			}
			else
			{
				// 进行攻击
				crtAttackid = target.showid;
				HeroCommandDevice.instance.updataCommand( new AttackCommand() );
			}
			
			// 释放引用对象
			target = null;
		}
		
		
		/**
		 * 获取手触对象 
		 * @param _x
		 * @param _y
		 * @return 
		 * 
		 */		
		private function getTouchTarget( touch:Touch ):DisplayObject
		{
			var p:Point = _scene.globalToLocal( new Point( touch.globalX, touch.globalY ) );
			for each( var dis:DisplayObject in _scene._eyeDic )
			{
				if( dis.bounds.contains( p.x, p.y ) )
				{
					touch = null;
					p = null;
					return dis;
				}
			}
			touch = null;
			p = null;
			return null;
		}
		
		/*-------------------------------public function---------------------------------*/
		
		/**
		 * 进入地图 
		 * @param mapId 地图id
		 */		
		public function enterMap( mapId:String ):void
		{
			if( mapId == null || mapId == "" || _scene.mapId == mapId ) return;
			_scene.enterMap( mapId );
		}
		
		
		/**
		 * 滚动地图 
		 * @param _x
		 * @param _y
		 * 
		 */		
		public function scrollMap( _x:Number, _y:Number ):void{ _scene.scrollMap( _x, _y )	};
		
		/**
		 * 设置场景点击感应开关 
		 * @param bol
		 * 
		 */		
		public function sceneTouchFlag( bol:Boolean ):void{ _sceneTouchFlag = bol };
		
		
		/**
		 * 获取场景引用对象 
		 * @return 
		 * 
		 */		
		public function get scene():GameScene{ return _scene };
		
		
		/**
		 * setter/getter 当前自身英雄攻击的目标id 
		 * @param val
		 * 
		 */		
		public function set crtAttackid( val:int ):void{ _crtAttackid = val };
		public function get crtAttackid():int{ return _crtAttackid };
		
		/**
		 * setter/getter 当前选中的目标id 
		 * @param val
		 * 
		 */		
		public function set crtSelectid( val:int ):void{ _crtSelectid = val };
		public function get crtSelectid():int{ return _crtSelectid };
		
		/**
		 * setter/getter 上一次选中的目标id 
		 * @param val
		 * 
		 */		
		public function set prevSelectid( val:int ):void{ _prevSelectid = val };
		public function get prevSelectid():int{ return _prevSelectid };
		
		
		/**
		 * 震屏 
		 * 
		 */		
		public function vibrationScreen():void
		{
			scene._vsFlag = true;
			scene.orgY = scene.y;
		}
		
	}
}